<?php
namespace app\system\controller;

use app\common\model\SpecialModel;
use think\Request;

class SpecialController extends BaseController
{

    private $specialModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->specialModel = new SpecialModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map            = [];
        $query          = [];
        $map['site_id'] = $this->getSiteId();
        $query['site_id'] = $this->getSiteId();
        $searchKey      = $this->request->param('searchKey');
        $searchText     = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
            $query['searchKey']  = $searchKey;
            $query['searchText'] = $searchText;
        }
        $list = $this->specialModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $result = $this->validate($data, 'SpecialValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->specialModel->addData($data)) {
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->specialModel->getError());
            }
        }
        $this->assign('utype',$this->getUtype());
        return $this->fetch();
    }

    /**
     * 修改数据
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'SpecialValidate');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->specialModel->editData($data)) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error($this->specialModel->getError());
            }
        }
        $specialId = $this->request->param('special_id');
        $info      = $this->specialModel->get($specialId);
        $this->assign('info', $info);
        $this->assign('utype',$this->getUtype());
        return $this->fetch();
    }

    /**
     * 删除数据
     */
    public function del()
    {
        $specialId = $this->request->param('special_id');
        if ($this->specialModel->delData($specialId)) {
            $this->success('删除成功！');
        } else {
            $this->error($this->specialModel->getError());
        }
    }

}
